/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.DAO.ServicesDAO;
import model.DAO.RoleSVDAO;
import model.DAO.RoomStatusDAO;
import model.entities.Services;
import model.entities.Role;
import model.entities.RoleSV;
import model.entities.Room;

/**
 *
 * @author Hieu.Nguyen
 */
public class QuanLyDichVu extends javax.swing.JPanel {

    /**
     * Creates new form QuangLyAmThuc
     */
    public QuanLyDichVu() {
        initComponents();
        loadTableServices(refreshCourseList());
        loadCBServicesRole();
        setEnableComponent(false);
       
    }
    public void loadCBServicesRole() {
            List<RoleSV> listRLSV = RoleSVDAO.getInstance().getRoleSV(true);
            for (RoleSV ro : listRLSV) {
            cboServicesRole.addItem(ro.getRoleSVID()+ "-" + ro.getRoleSVName());
        }
    }
    
    public List<Services> refreshCourseList() {
        
       List<Services> servicesList;
       servicesList = model.DAO.ServicesDAO.getInstance().getServices(true);

        return servicesList;
    }

    public void loadTableServices(List<Services> list) {
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        String[] colNames = {"ServicesID", "ServicesName","RoleServices", "ServicesAmount"};
        dtm.setColumnIdentifiers(colNames);
        dtm.setNumRows(0);

        for (Services stud : list) {
            String[] cells = new String[4];
            cells[0] = String.valueOf(stud.getServicesID());
            cells[1] = stud.getServicesName();
            cells[2] = stud.getRoleSV().getRoleSVName();
            cells[3] = String.valueOf(stud.getServicesAmount());
            dtm.addRow(cells);
        }
    }
    public void binding() {
        try {
            int rowIndex = jTable1.getSelectedRow();
            String sevicesID = jTable1.getValueAt(rowIndex, 0).toString();
            txtServicesID.setText(sevicesID);
            String svname = (String) jTable1.getValueAt(rowIndex, 1);
            txtServicesName.setText(svname);
            
            int roleSVID = ServicesDAO.getInstance().getServicesByID(Integer.parseInt(sevicesID)).getRoleSV().getRoleSVID();
            String roleSV = ServicesDAO.getInstance().getServicesByID(Integer.parseInt(sevicesID)).getRoleSV().getRoleSVName();
            cboServicesRole.setSelectedItem(roleSVID + "-" + roleSV);
           
//            
            String des = (String) jTable1.getValueAt(rowIndex, 3);
            txtServicesAmount.setText(des);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setEnableComponent(boolean isTrue) {
        txtServicesID.setEnabled(false);
        txtServicesName.setEnabled(isTrue);
        txtServicesAmount.setEnabled(isTrue);
        cboServicesRole.setEnabled(isTrue);
        btnCanel.setEnabled(isTrue);
        btnDelete.setEnabled(isTrue);
        btnSave.setEnabled(isTrue);
        btnEdit.setEnabled(isTrue);

        
    }
    void resetComponent() {
        txtServicesID.setText("");
        txtServicesName.setText("");
        txtServicesAmount.setText("");
    }
 

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtServicesID = new javax.swing.JTextField();
        txtServicesName = new javax.swing.JTextField();
        txtServicesAmount = new javax.swing.JTextField();
        cboServicesRole = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btnCanel = new javax.swing.JButton();

        setBackground(new java.awt.Color(204, 204, 0));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("Services Management");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ServicesID", "ServicesName", "ServicesRole", "ServicesAmount"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        btnAdd.setText("AddServices");
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDeleteMouseClicked(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jLabel2.setText("ServicesID      :");

        jLabel3.setText("ServicesName :");

        jLabel4.setText("ServicesRole        :");

        jLabel5.setText("ServicesAmount   :");

        btnSave.setText("Save");
        btnSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSaveMouseClicked(evt);
            }
        });

        btnCanel.setText("Canel");
        btnCanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtServicesID)
                            .addComponent(txtServicesName, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtServicesAmount)
                            .addComponent(cboServicesRole, 0, 160, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19))))
            .addGroup(layout.createSequentialGroup()
                .addGap(300, 300, 300)
                .addComponent(jLabel1)
                .addGap(0, 310, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(txtServicesID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboServicesRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(txtServicesName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtServicesAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDeleteMouseClicked
        // TODO add your handling code here:
        int rowIndex = jTable1.getSelectedRow();
        //
        String strID = (String) jTable1.getValueAt(rowIndex, 0);
        int id = Integer.parseInt(strID);
        
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int i = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa dich vu");
        if (i == JOptionPane.YES_OPTION) {
            
                ServicesDAO.getInstance().deleteServices(id);
            
            JOptionPane.showMessageDialog(this, "Đã xóa dich vu");
            loadTableServices(refreshCourseList());
        } else {
            remove(dialogButton);
        }
    }//GEN-LAST:event_btnDeleteMouseClicked

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        // TODO add your handling code here:
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        binding();
    }//GEN-LAST:event_jTable1KeyReleased

    private void btnSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSaveMouseClicked
        // TODO add your handling code here:
        
        
        String name= txtServicesName.getText();
        
        String svam = txtServicesAmount.getText();
        float sva = Float.parseFloat(svam);

        String roleSV = cboServicesRole.getSelectedItem().toString();
        String[] arr2 = roleSV.split("-");
        int idRoleDV = Integer.parseInt(arr2[0]); 
        RoleSV rst = RoleSVDAO.getInstance().getRoleSVByID(idRoleDV);
        
        if (txtServicesID.getText().equals("")) {
            Services servs = new Services(name, rst, sva, true);
            if (ServicesDAO.getInstance().createServices(servs)) {
                JOptionPane.showMessageDialog(null, "Thêm mới thành công");
            } else {
                JOptionPane.showMessageDialog(null, "Lỗi");
            }
            resetComponent();
            setEnableComponent(false);
            loadTableServices(refreshCourseList());
        } else {
            String strIDCourse = txtServicesID.getText();
            int id = Integer.parseInt(strIDCourse);
            Services sv = new Services(id, name, rst, sva, true);
            if (ServicesDAO.getInstance().updateRoom(sv)) {
                JOptionPane.showMessageDialog(null, "Cập nhật thành công");
            }else{
                JOptionPane.showMessageDialog(null, "Có lỗi xảy ra");
            }
            resetComponent();
            loadTableServices(refreshCourseList());
            setEnableComponent(false);
        }
    }//GEN-LAST:event_btnSaveMouseClicked

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        // TODO add your handling code here:
        resetComponent();
        setEnableComponent(true);
    }//GEN-LAST:event_btnAddMouseClicked

    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEditMouseClicked
        // TODO add your handling code here:
        setEnableComponent(true);
    }//GEN-LAST:event_btnEditMouseClicked

    private void btnCanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCanelMouseClicked
        // TODO add your handling code here:
        setEnableComponent(false);
    }//GEN-LAST:event_btnCanelMouseClicked

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        binding();
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCanel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cboServicesRole;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtServicesAmount;
    private javax.swing.JTextField txtServicesID;
    private javax.swing.JTextField txtServicesName;
    // End of variables declaration//GEN-END:variables
}
